define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order_change/index' + location.search,
                    add_url: 'order_change/add',
                    // edit_url: 'order_change/edit',
                    del_url: 'order_change/del',
                    multi_url: 'order_change/multi',
                    table: 'order_change',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'order_id', title: __('Order_id')},
                        {field: 'out_trade_no', title: __('Out_trade_no')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'rental_money', title: __('Rental_money'), operate:'BETWEEN'},
                        // {field: 'deposit', title: __('Deposit'), operate:'BETWEEN'},
                        {field: 'all_money', title: __('All_money'), operate:'BETWEEN'},
                        {field: 'diff_money', title: __('Diff_money'), operate:'BETWEEN'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: Config.payTypeList, formatter: Table.api.formatter.label},
                        {field: 'type', title: __('Type'), searchList: {1:'否', 2:'是'}, formatter: Table.api.formatter.status},
                        {field: 'pay_status', title: __('Pay_status'), searchList: Config.statusList, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'order.out_trade_no', title: __('Total_trade_no')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name:'cancel',
                                    text:'取消订单',
                                    title:'取消订单',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url: 'order_change/cancel',
                                    visible: function (row) {
                                        if((row.pay_status == 1 || row.pay_status == 2) && row.order.status == 1) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                        Toastr.success("取消成功");
                                        layer.close(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                // {
                                //     name:'return',
                                //     text:'退款',
                                //     title:'退款',
                                //     classname:'btn btn-xs btn-danger btn-dialog',
                                //     url: 'order_change/return',
                                //     visible: function (row) {
                                //         if(row.pay_status == 2 && row.type == 1) return true;
                                //         return false;
                                //     },
                                //     success:function (res){
                                //         table.bootstrapTable('refresh');
                                //         Toastr.success("退款成功");
                                //         layer.close(index);
                                //         return false;
                                //     },
                                //     error: function (data, ret) {
                                //         return false;
                                //     },
                                // },
                                {
                                    name:'detail',
                                    text:'订单详情',
                                    title:'订单详情',
                                    icon:'fa fa-list',
                                    classname:'btn btn-xs btn-primary btn-dialog',
                                    url: 'order_change/detail'
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
            $(document).on("change", "#c-merchant_car_id", function(){
                var merchant_car_id = $("#c-merchant_car_id").val();
                var type = $("input[name='row[type]']:checked").val();
                Fast.api.ajax({
                    url: 'order_change/summoney',
                    data: {merchant_car_id: merchant_car_id, order_id:$("#order_id").val(), type:type},
                    type: 'POST',
                    loading:false,
                    success: function (res){
                        console.log(res);
                        $('#new_car_image').attr('src', res.data.car_image);
                        $('#new_car_name').text(res.data.car_name);
                        $('#new_car_number').text(res.data.car_number);
                        $('#new_car_price').text(res.data.car_price);
                        $("#c-diff_money").val(res.data.diff_money);
                        return false;
                    }
                });
            });

            $(document).on("change", "#car_type", function(){
                var type = $("input[name='row[type]']:checked").val();
                checkStatus(type);
            });
            function checkStatus(type){
                if(type == 1){
                    $("#other_number").show();
                }else{
                    $("#other_number").hide();
                }
            }
        },
        edit: function () {
            Controller.api.bindevent();
        },
        return: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});